/**
 * @date 11:04:14 AM
 * EducationSubjectLessonDaoImpl.java
 * Administrator
 * TODO
 */
package com.qs.education.bean.dao.impl;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Repository;

import com.qs.education.bean.dao.IEducationSubjectLessonDao;
import com.qs.education.bean.entity.EducationQuestion;
import com.qs.education.bean.entity.EducationSubjectLesson;

/**
 * @class EducationSubjectLessonDaoImpl
 * @author Warren
 * @description
 * @date Nov 20, 2018
 */
@Repository
public class EducationSubjectLessonDaoImpl extends BaseDaoImpl<EducationSubjectLesson>
		implements IEducationSubjectLessonDao {

	/**
	 * @Description 根据课时查询课时下所有题目的数量
	 * @author Leon
	 * @date Nov 20, 2018
	 * @param
	 * @return Integer
	 */
	@Override
	public Integer selectQuestionCount(Long id) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("id", id);
		String jql = "Select Count(*) from EducationQuestion ea where ea.lesson.id =:id";
		List<?> result = query(jql, params);
		if (result != null && !result.isEmpty()) {
			return Integer.parseInt(result.get(0).toString());
		}
		return 0;
	}

	/**
	 * @Description 根据选择的课时 题目数量 场景 以及 题目难度进行 智能组题
	 * @author Leon
	 * @date Nov 21, 2018
	 * @param params：[ {lessonId:1,number:2}, {lessonId:2,number:3} ] scenes: 场景
	 *        level ： 等级
	 * @return ResponseListBean<EducationQuestion>
	 */
	@Override
	public List<EducationQuestion> getQuestionListByLesson(List<String> lessonIds, List<String> numbers, String scenes,
			String level) {
		List<EducationQuestion> allQuestion = new ArrayList<EducationQuestion>();
		// 1. 先根据课时id以及等级获取所有题目的数量
		// 2. 如果题目数量大于所需要的题目数量则直接随机抽取
		// 3. 如果题目数量小于所需要的题目数量则 根据课时id以及下一等级获取所有题目的数量
		for (int a = 0; a < lessonIds.size(); a++) {
			Long id = Long.parseLong(lessonIds.get(a));
			Long number = Long.parseLong(numbers.get(a));
			Map<String, Object> params = new HashMap<String, Object>();
			params.put("Id", id);
			Long num = 0l;
			for (int lev = Integer.parseInt(level); lev >= 1; lev--) {
				params.put("level", lev);
				String jql = "Select COUNT(*) from EducationQuestion ea where ea.lesson.id =:Id and level = :level";
				List<?> result = query(jql, params);
				if (result != null && !result.isEmpty()) {
					//  获取 课时id 以及该等级获取所有题目的数量
					num = Long.parseLong(result.get(0).toString());
					System.out.println("课时ID: "+id+" 题目总数量  "+num+" 等级  ："+lev);
					System.out.println("需要题目数量：  "+number);
				}
				//  判断题目数量与所需要的题目数量相比较
				//  如果大于则直接抽取题目
				if (num >= number) {
					System.out.println("num>number 直接在该等级下全部抽取");
					String jql1 = "Select ea from EducationQuestion ea where ea.lesson.id =:Id and level = :level order by RAND() ";
					List<EducationQuestion> question = query(jql1, EducationQuestion.class, 0, Integer.parseInt(number+""), params);
					for(EducationQuestion forQuestion :question) {
						forQuestion.setAnswerList(null);
						forQuestion.setHomeWorkQuestionList(null);
						forQuestion.setLesson(null);
					}
					allQuestion.addAll(question);
					//  跳出等级循环 进入 课时循环
					break; 
				}
				//  如果小于则 先将 该 等级的所有题目抽取
				//  然后再去抽取下一等级的题目
				if (num < number) {
					//  抽取该等级下的所有题目
					System.out.println("num<number 抽取该等级下全部题目后抽取下一等级题目");
					String jql2 = "Select ea from EducationQuestion ea where ea.lesson.id =:Id and level = :level";
					List<EducationQuestion> question = query(jql2, EducationQuestion.class, params);
					for(EducationQuestion forQuestion :question) {
						forQuestion.setAnswerList(null);
						forQuestion.setHomeWorkQuestionList(null);
						forQuestion.setLesson(null);
					}
					allQuestion.addAll(question);
					number -= num;
				}
			}
		}
		Collections.reverse(allQuestion);
		return allQuestion;
	}
	
	/**
	 * @Description 根据lessonIds 批量查询 Education
	 * @author Leon
	 * @date   Nov 23, 2018
	 * @param  
	 * @return List<EducationSubjectLesson>
	 */
	@Override
	public List<EducationSubjectLesson> getLessonByIds(List<Long> lessonIds) {
		String jql = "Select ea from EducationSubjectLesson ea where ea.id in (:ids)";
		Map<String,Object> params = new HashMap<String,Object>();
		params.put("ids", lessonIds);
		List<EducationSubjectLesson> lessons = query(jql, EducationSubjectLesson.class, params);
		return lessons;
	}
}
